﻿using System;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.IO;
using System.Windows.Forms;
using Infragistics.Win.AppStyling;
using ScriptEase.Shell.Controls;
using ScriptEase.Shell.Properties;



namespace ScriptEase.Shell
{
    internal class Bootstrapper
    {
        public void Run()
        {
            SplashScreen splash = new SplashScreen();
            try
            {
                splash.Start();

                System.Threading.Tasks.Task t2 = System.Threading.Tasks.Task.Factory.StartNew(
                   () =>
                   {
                       Trace.Default.Debug("Start loading styles..");
                       Application.EnableVisualStyles();
                       StyleManager.Load(new MemoryStream(Resources.ApplicationStyle_blue));
                       Trace.Default.Debug("Styles loaded!");
                   });


                System.Threading.Tasks.Task t1 = System.Threading.Tasks.Task.Factory.StartNew(() =>
                    {
                        Trace.Default.Debug("App starts from directory={0}", Program.BinDirectory);
                        //An aggregate catalog that combines multiple catalogs
                        var catalog = new AggregateCatalog();

                        catalog.Catalogs.Add(new AssemblyCatalog(typeof(Program).Assembly));
                        catalog.Catalogs.Add(new DirectoryCatalog(@".", "Microsoft.Practices.*.dll"));
                        catalog.Catalogs.Add(new DirectoryCatalog(@".", "ScriptEase.*.dll"));
                        //Create the CompositionContainer with the parts in the catalog
                        ServiceLocator.Container = new CompositionContainer(catalog);
                        //Fill the imports of this object
                        ServiceLocator.Container.ComposeParts(this);
                        Trace.Default.Debug("MEF initialized!");
                    });

                t1.Wait();
                t2.Wait();

                Trace.Default.Debug("Initialize ShellView...");
                ShellView shellView = new ShellView(); // 3 sec!
                //Trace.Default.Debug("ShellView show()");
                shellView.Show();
                Application.DoEvents();

                Trace.Default.Debug("Initialize ShellViewModel...");
                ShellViewModel viewModel = ServiceLocator.GetInstance<ShellViewModel>();
                shellView.Initialize(viewModel);
                viewModel.Initialize(shellView);
                Trace.Default.Debug("ShellViewModel initialized");

                splash.Stop();

                Application.Run(shellView);
            }
            catch (Exception ex)
            {
                splash.Stop();
                Trace.Default.LogException(ex);
                PublicFunc.DisplayUnexpectedApplicationTerminationDialog(ex);
            }


            Trace.Default.Info("Application Main ended");
            Application.Exit();
        }
    }
}